#!/usr/bin/env jython
#-*- coding: utf-8 -*-

''' Author: Susana Messias n.º 6046 '''

from javax.swing import JFrame, JButton, JComboBox, WindowConstants
from javax.swing import JMenuBar, JMenu, JMenuItem, JPanel
from csv_import_jython import createDatabase
from statistics_jython import Statistics
from java.lang import System
from java.awt import BorderLayout, GridLayout

class createCSV1:
    '''
    Creates the window that generate the CSV file with the choosen information
    '''
    def __init__(self, data):
        self.data = data
        self.frame = JFrame("Teachers per establishment per year")
        self.frame.setLayout(BorderLayout())


        establishmentList = []
        for name in self.data.establishment:
            establishmentList.append(name.decode('utf-8'))
            pass
        
        self.comboBox = JComboBox(establishmentList)

        
        button = JButton('Create CSV',actionPerformed=self.evt_saveCSVStats)

        cbPanel = JPanel()
        cbPanel.add(self.comboBox, BorderLayout.CENTER)
        
        self.frame.add(cbPanel, BorderLayout.CENTER)
        
        self.frame.add(button, BorderLayout.SOUTH)
              
        self.frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
        self.frame.pack()
        self.frame.visible = True
        pass

    def evt_exit(self, event):
        self.frame.dispose()
        pass

    def evt_saveCSVStats(self, event):
        # event that calls a method to save the statistics in a CSV file
        self.data.creatCSV1( self.data.establishment[self.comboBox.getSelectedIndex()] )
        pass
    pass


'''
Creates the window that generate the CSV file with the choosen information
'''
class createCSV2:
    def __init__(self, data):
        self.data = data
        self.frame = JFrame("Teachers divided by degree per establishment per year")
        self.frame.setLayout(BorderLayout())

        establishmentList = []
        for name in self.data.establishment:
            establishmentList.append(name.decode('utf-8'))
            pass

        degreeList = []
        for name in self.data.degree:
            degreeList.append(name.decode('utf-8'))
            pass

        self.comboBox = JComboBox(establishmentList)
        self.comboBox2 = JComboBox(degreeList)
        
        button = JButton('Create CSV',actionPerformed=self.evt_saveCSV2stats)

        cbPanel = JPanel()
        cbPanel.add(self.comboBox, BorderLayout.CENTER)
        cbPanel.add(self.comboBox2, BorderLayout.CENTER)
        
        self.frame.add(cbPanel, BorderLayout.CENTER)
        
        self.frame.add(button, BorderLayout.SOUTH)
              
        self.frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
        self.frame.pack()
        self.frame.visible = True
        pass


    def evt_saveCSV2stats(self, event):
        establishment = self.data.establishment[self.comboBox.getSelectedIndex()]
        degree = self.data.degree[self.comboBox2.getSelectedIndex()]        
        self.data.creatCSV3(establishment, degree)
        pass
    pass


'''
Creates the window that generate the CSV file with the choosen information
'''
class createCSV3:
    def __init__(self, data):
        self.data = data
        self.frame = JFrame("Teachers divided by category per establishment per year")
        self.frame.setLayout(BorderLayout())

        establishmentList = []
        for name in self.data.establishment:
            establishmentList.append(name.decode('utf-8'))
            pass

        categorieList = []
        for name in self.data.categorie:
            categorieList.append(name.decode('utf-8'))
            pass

        self.comboBox = JComboBox(establishmentList)
        self.comboBox2 = JComboBox(categorieList)

        button = JButton('Create CSV',actionPerformed=self.evt_saveCSV3Stats)

        cbPanel = JPanel()
        cbPanel.add(self.comboBox, BorderLayout.CENTER)
        cbPanel.add(self.comboBox2, BorderLayout.CENTER)
        
        self.frame.add(cbPanel, BorderLayout.CENTER)
        
        self.frame.add(button, BorderLayout.SOUTH)
              
        self.frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
        self.frame.pack()
        self.frame.visible = True
        pass            

    def evt_saveCSV3Stats(self, event):
        establishment = self.data.establishment[self.comboBox.getSelectedIndex()]
        categorie = self.data.categorie[self.comboBox2.getSelectedIndex()]        
        self.data.creatCSV2(establishment, categorie)
        pass

'''
Creates the main window
'''
class interface:
    
    def close_me(self, event):
        System.exit(0)
        pass

    def open_database(self, event):
        createDatabase.create()
        self.data = Statistics()
        pass

    def number_teacher_categories_establishment_year(self, event):
        createCSV2(self.data)
        pass

    def number_teacher_degree(self, event):
        createCSV3(self.data)
        pass

    def number_teacher_educationSystem_establishmente_year(self, event):
        createCSV1(self.data)
        pass

    def evt_HTMLstats1(self, event):
        self.data.firstHTML()
        pass

    def evt_HTMLstats2(self, event):
        self.data.secondHTML()
        pass

    def evt_HTMLstats3(self, event):
        self.data.thirdHTML()
        pass


        #constructor, creates the main window.
    def __init__ (self):
        self.frame = JFrame(u"Dados sobre o Ensino Superior Português",
                       defaultCloseOperation = JFrame.EXIT_ON_CLOSE)

        menubar = JMenuBar()
        ##### FILE #####
        menu = JMenu ("File")
        menuitem_create = JMenuItem("Create/Open Database", actionPerformed=self.open_database)
        menu.add(menuitem_create)
        menuitem_exit = JMenuItem("Exit", actionPerformed=self.close_me)
        menu.add(menuitem_exit)
        menubar.add(menu)

        ##### STATISTICS ##### Numbers of teachers divided by degree
        menuStats = JMenu ("Statistics")
        menuStatsitem_first = JMenuItem("Numbers of teachers divided by degree", actionPerformed=self.number_teacher_categories_establishment_year)
        menuStats.add(menuStatsitem_first)
        menuStatsitem_second = JMenuItem("Numbers of teachers divided by categories per establhisment and per year", actionPerformed=self.number_teacher_degree)
        menuStats.add(menuStatsitem_second)
        menuStatsitem_third = JMenuItem("Numbers of teachers in the higher education system per establhisment per year", actionPerformed=self.number_teacher_educationSystem_establishmente_year)
        menuStats.add(menuStatsitem_third)
        menubar.add(menuStats)

        ##### HTML ######
        menuHtml = JMenu ("HTML")
        menuHtmlitem_stats = JMenu("Statistics")
        menuHtml.add(menuHtmlitem_stats)
        menuHtmlitem_first = JMenuItem("Numbers of teachers divided by categories per establhisment and per year", actionPerformed=self.evt_HTMLstats2)
        menuHtmlitem_stats.add(menuHtmlitem_first)
        menuHtmlitem_second = JMenuItem("Numbers of teachers divided by degree", actionPerformed=self.evt_HTMLstats3)
        menuHtmlitem_stats.add(menuHtmlitem_second)
        menuHtmlitem_third = JMenuItem("Numbers of teachers in the higher education system per establhisment per year", actionPerformed=self.evt_HTMLstats1)
        menuHtmlitem_stats.add(menuHtmlitem_third)

        menubar.add(menuHtml)

        self.frame.setJMenuBar(menubar)
        self.frame.pack()
        self.frame.visible = True


if __name__ == '__main__':
    interface()


